package com.yaozheng.vocationaltraining.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间帮助类
 *
 * @author Administrator
 */
public class DateUtils {

    public static final String[] zodiacArr = {"猴", "鸡", "狗", "猪", "鼠", "牛",
            "虎", "兔", "龙", "蛇", "马", "羊"};

    public static final String[] constellationArr = {"水瓶座", "双鱼座", "白羊座",
            "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"};

    public static final int[] constellationEdgeDay = {20, 19, 21, 21, 21, 22,
            23, 23, 23, 23, 22, 22};

    /**
     * 得到年龄
     *
     * @param birthDay
     * @return
     * @throws Exception
     */
    public static int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            // throw new IllegalArgumentException(
            // "The birthDay is before Now.It's unbelievable!");
            return -1;
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth)
                    age--;
            } else {
                age--;
            }
        }
        return age;
    }

    /**
     * 根据日期获取生肖
     *
     * @return
     */
    public static String getZodica(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return zodiacArr[cal.get(Calendar.YEAR) % 12];
    }

    /**
     * 根据日期获取星座
     *
     * @return
     */
    public static String getConstellation(Date date) {
        if (date == null) {
            return "";
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        if (day < constellationEdgeDay[month]) {
            month = month - 1;
        }
        if (month >= 0) {
            return constellationArr[month];
        }
        // default to return 魔羯
        return constellationArr[11];
    }


    /**
     * 得到两个时间相差的小时,分,秒
     * SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
     * "yyyy-MM-dd HH:mm:ss");
     * Date date1 = simpleDateFormat.parse("2016-06-02 11:37:20");
     * Date date2 = simpleDateFormat.parse("2016-06-02 13:38:21");
     * long dateLong1 = date1.getTime();
     * long dateLong2 = date2.getTime();
     * 2小时1分1秒
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static String getHourAndMinuteAndSecond(long startDate, long endDate) {
        long surplusLong = endDate - startDate;
        long hour = surplusLong / 1000 / 60 / 60;
        long minute = surplusLong / 1000 / 60 % 60;
        long second = surplusLong / 1000 % 60;
        StringBuffer returnStringBuffer = null;
        if (hour > 0) {
            if (returnStringBuffer == null) {
                returnStringBuffer = new StringBuffer();
            }
            returnStringBuffer.append(hour);
            returnStringBuffer.append("小时");
        }
        if (minute > 0) {
            if (returnStringBuffer == null) {
                returnStringBuffer = new StringBuffer();
            }
            returnStringBuffer.append(minute);
            returnStringBuffer.append("分");
        }
        if (second > 0) {
            if (returnStringBuffer == null) {
                returnStringBuffer = new StringBuffer();
            }
            returnStringBuffer.append(second);
            returnStringBuffer.append("秒");
        }
        if (returnStringBuffer == null) {
            return "0秒";
        } else {
            return returnStringBuffer.toString();
        }
        // if (second > 0 && minute <= 0) {
        // return second + "秒";
        // } else if (second > 0 && minute > 0) {
        // return minute + "分" + second + "秒";
        // } else if (second <= 0 && minute > 0) {
        // return minute + "分";
        // } else {
        // return "0秒";
        // }
    }
}
